var express = require('express');
var router = express.Router();
var setToken = require('../public/javascripts/token')
var {queryTableByParams,customQuery} = require('../mysql/index')
const moment = require("moment/moment");
/* GET home page. */
router.get('/', function (req, res, next) {
    console.log('index')
    res.render('index', {title: 'Express'});
});

router.get('/admin', function (req, res, next) {
    console.log('index')
    res.render('admin', {title: 'Express'});
});

// 登录
router.post('/login', function (req, res, next) {
    const {user_name, password,isAdmin} = req.body;
    console.log('isAdmin',isAdmin)
    queryTableByParams('user_list', {user_name, passcode:password}).then(data => {
        console.log(data[0])
        if (data.length === 0) {
            res.status(200).send({code:403,msg:'用户名或者密码错误'});
        }else if(isAdmin&&data[0].role_id!==1){
            res.status(200).send({code:403,msg:'权限不够'});
        } else {
            const user = data[0];
            const {id, user_name} = user
            const {password,passcode,note,...userWithoutPasscodeAndNote} = user;
            setToken.setToken(user_name, id).then((data) => {
                return res.status(200).send({
                    code: 200, data: {
                        token: data,
                        user:userWithoutPasscodeAndNote
                    }
                })
            })

        }
    })
});
//修改密码
router.post('/password', function (req, res, next) {
    console.log('passwordpasswordpassword')
    const {user_name,password, pass,passcode} = req.body;
    queryTableByParams('user_list', {user_name, password}).then(data => {
        console.log('data',data)
        if (data.length === 0) {
            console.log(55555555)
            res.status(200).send({code:403,msg:'原密码错误'});
        } else {
            const {id} = data[0]
            console.log(6666666666666,passcode)
            const sql = `UPDATE user_list SET password='${pass}', passcode='${passcode}' WHERE id=${id}`;
            customQuery(sql).then(r=>{
                console.log(r)
                res.status(200).send({code:200,msg:'修改成功'});
            })
        }
    })

});

router.post('/register', function (req, res, next) {
    // 解构请求体中的参数
    const {role_name, user_code='', user_name, password, phone, email, qq, note='', role_id,real_name='',passcode} = req.body;
    const create_time = moment().format('YYYY-MM-DD HH:mm:ss');
    // 构造查询语句
    const sql = `SELECT * FROM user_list WHERE user_name='${user_name}'`;
    const sql1 = `SELECT * FROM user_list WHERE role_name='${role_name}'`;
    Promise.all([customQuery(sql), customQuery(sql1)]).then(results => {
        if (results[0].length !== 0) {
            return res.status(200).send({code: 301, msg: '账号已存在'});
        }
        if (results[1].length !== 0) {
            return res.status(200).send({code: 301, msg: '用户名已存在'});
        }
        const insertSql = `INSERT INTO user_list (role_name,user_code,user_name,password,phone,email,qq,note,role_id,create_time,passcode,real_name) VALUES ('${role_name}', '${user_code}', '${user_name}', '${password}', '${phone}', '${email}', '${qq}', '${note}', '${role_id}','${create_time}','${passcode}','${real_name}')`;
        customQuery(insertSql).then(results => {
            return res.status(200).send({code: 200, msg: '操作成功', list: results});
        })
    })
});





module.exports = router;
